package com.tqyan.start.controller;

import com.tqyan.model.Result;
import com.tqyan.model.controller.request.UserRequest;
import com.tqyan.model.controller.response.UserResp;
import com.tqyan.service.service.UserService;
import com.tqyan.service.transfer.LimitService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description 用户API
 * <p> <功能详细描述> </p>
 *
 * @author 陈晨
 * @date 2023/4/11 17:20
 */
@RestController
@RequestMapping("/user/")
@Slf4j
//@Api(tags = "用户API")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private LimitService limitService;

    @PostMapping("/getInfo")
//    @ApiOperation(value = "获取用户信息")
    public Result<UserResp> getUserInfo(@RequestBody UserRequest userRequest) {
        if (null == userRequest
                || StringUtils.isBlank(userRequest.getOpenId())) {
            return Result.fail("请求参数不可为空");
        }
//        log.info("[微信] userRequest={}, 获取用户信息", userRequest);
        boolean isLock = limitService.lock(userRequest.getOpenId());
        if (!isLock) {
            return Result.fail("让小燕子休息一会吧");
        }
        try {
            return Result.success(userService.getUserInfoAll(userRequest.getOpenId()));
        } catch (Exception e) {
            log.error("[微信] userRequest={}, 获取用户信息, 异常: {}"
                    , userRequest, e.getMessage(), e);
            return Result.fail(e.getMessage());
        } finally {
            limitService.unlock(userRequest.getOpenId());
        }
    }

    @PostMapping("/setModel")
//    @ApiOperation(value = "设置会话模式")
    public Result<Void> setModel(@RequestBody UserRequest userRequest) {
        if (null == userRequest
                || StringUtils.isBlank(userRequest.getOpenId())) {
            return Result.fail("请求参数不可为空");
        }
        log.info("[用户设置] userRequest={}, 设置会话模式", userRequest);
        try {
            userService.setModel(userRequest.getOpenId(), userRequest.getModel());
            return Result.success();
        } catch (Exception e) {
            log.error("[用户设置] userRequest={}, 设置会话模式, 异常: {}"
                    , userRequest, e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/setCreativeAbility")
//    @ApiOperation(value = "设置创造力")
    public Result<Void> setCreativeAbility(@RequestBody UserRequest userRequest) {
        if (null == userRequest
                || StringUtils.isBlank(userRequest.getOpenId())) {
            return Result.fail("请求参数不可为空");
        }
        log.info("[用户设置] userRequest={}, 设置创造力", userRequest);
        try {
            userService.setCreativeAbility(userRequest.getOpenId(), userRequest.getCreativeAbility());
            return Result.success();
        } catch (Exception e) {
            log.error("[用户设置] userRequest={}, 设置创造力, 异常: {}"
                    , userRequest, e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/setOpenAiKey")
//    @ApiOperation(value = "设置OpenAIKey")
    public Result<Void> setOpenAiKey(@RequestBody UserRequest userRequest) {
        if (null == userRequest
                || StringUtils.isBlank(userRequest.getOpenId())) {
            return Result.fail("请求参数不可为空");
        }
        log.info("[用户设置] userRequest={}, 设置OpenAIKey", userRequest);
        try {
            userService.setOpenAiKey(userRequest.getOpenId(), userRequest.getOpenAiKey());
            return Result.success();
        } catch (Exception e) {
            log.error("[用户设置] userRequest={}, 设置OpenAIKey, 异常: {}"
                    , userRequest, e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

}


